<?php
/**
 * Created by PhpStorm.
 * User: 87393
 * Date: 2019-02-24
 * Time: 12:02
 */

namespace App\Http\Controllers\Admin\View;


use App\Http\Controllers\Controller;
use App\Http\Repositories\Services\IArticle;
use App\Http\Repositories\Services\IType;
use App\Http\Requests\Admin\ArticleInputDTO;
use App\Util\ResultsVo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;

class ArticleController extends Controller
{

    protected $article;
    protected $typeService;

    public function __construct(IArticle $article, IType $typeService)
    {
        $this->article = $article;
        $this->typeService= $typeService;
    }

    public function page(){
        $tree = $this->typeService->findTree();
        return view('admin.article.index',['tree'=>$tree]);
    }

    public function data(Request $request){
        $search = [];
        $dao = $this->article->paginate($search,$request->get('limit','10'));
        return response()->json(new ResultsVo(['data'=>$dao,'message'=>Lang::get('response.success')]));
    }

    public function store(){
        $tree = $this->typeService->findTree();
        return view('admin.article.create',['tree'=>$tree]);
    }

    public function edit($id){
        $tree = $this->typeService->findTree();
        $dao = $this->article->byId($id);
        return view('admin.article.edit',['tree'=>$tree,'dao'=>$dao]);
    }

    public function update(ArticleInputDTO $inputDTO,$id){
        $this->article->update($inputDTO,$id);
        return response()->json(new ResultsVo(['data'=>true,'message'=>Lang::get('response.success'),'jump'=>'/admin/article']));
    }

    public function state(Request $request){
        $ids = $request->get('ids');
        $val = $request->get('state');
        $this->article->state($ids,$val);
        return response()->json(new ResultsVo(['data'=>true,'message'=>Lang::get('response.success')]));
    }
}